﻿// 功能：图标工具条控件
// 描述：
// 作者：温伟鹏
// 日期：2010/6/8 11:03:04

using System;
using System.Runtime.InteropServices;
using System.Drawing;
using MeizuSDK.Drawing;

namespace MeizuSDK.Presentation
{
    /// <summary>
    /// 图标工具条控件
    /// </summary>
    public class IconToolBar : Control
    {
        #region DllImport
        [DllImport("MZFCWrapper.dll")]
        private static extern IntPtr UiToolbar_IconCtor();

        [DllImport("MZFCWrapper.dll")]
        private static extern int UiToolbar_IconCalcIndexOfPos(IntPtr pObj, int x, int y);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiToolbar_IconCalcIndexOfPos(IntPtr pObj, int nIndex, bool bEnable);

        [DllImport("MZFCWrapper.dll")]
        private static extern int UiToolbar_IconGetButtonCount(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern int UiToolbar_IconGetButtonHighLightGrade(IntPtr pObj, int nIndex);

        [DllImport("MZFCWrapper.dll")]
        private static extern int UiToolbar_IconGetHighLightButton(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern int UiToolbar_IconGetIconBarType(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool UiToolbar_IconIsButtonEnabled(IntPtr pObj, int nIndex);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiToolbar_IconLoadButtonIcon(IntPtr pObj, int nIndex, IntPtr hInst, 
            [In][MarshalAs(UnmanagedType.LPWStr)]string lpResType,
            [In][MarshalAs(UnmanagedType.LPWStr)]string lpResName);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiToolbar_IconLoadButtonIcon_Disabled(IntPtr pObj, int nIndex, IntPtr hInst,
            [In][MarshalAs(UnmanagedType.LPWStr)]string lpResType, 
            [In][MarshalAs(UnmanagedType.LPWStr)]string lpResName);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiToolbar_IconSetButtonCount(IntPtr pObj, int nCount);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiToolbar_IconSetButtonHighLightGrade(IntPtr pObj, int nIndex, int nGrade);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiToolbar_IconSetHightLightButton(IntPtr pObj, int nIndex);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiToolbar_IconSetIconBarType(IntPtr pObj, int nType);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiToolbar_IconShowButton(IntPtr pObj, int nIndex, bool bShow);
        #endregion

        #region 事件
        /// <summary>
        /// 当用户单击工具条上按钮时触发此事件
        /// </summary>
        public event IconToolBarButtonClickedEventHandler ButtonClicked;
        #endregion

        #region 常量
        /// <summary>
        /// 获取工具栏高度
        /// </summary>
        public const int HEIGHT = MzMetrics.MZM_HEIGHT_TOOLBARPRO;
        #endregion

        #region 私有变量
        /// <summary>
        /// 默认按钮数量
        /// </summary>
        private int defaultButtonCount = 4;
        /// <summary>
        /// 默认高亮的按钮索引
        /// </summary>
        private int highlightButtonIndex = -1;
        #endregion

        #region 公共属性
        /// <summary>
        /// 获取或设置工具条中按钮数量。默认为4个按钮
        /// </summary>
        public int ButtonCount
        {
            get
            {
                if (IsHandleCreated)
                {
                    defaultButtonCount = UiToolbar_IconGetButtonCount(Handle);
                }
                return defaultButtonCount;
            }
            set
            {
                if (value != defaultButtonCount)
                {
                    defaultButtonCount = value;

                    if (IsHandleCreated)
                    {
                        UiToolbar_IconSetButtonCount(Handle, value);
                    }
                }
            }
        }
        /// <summary>
        /// 获取或设置工具条默认高亮的按钮索引
        /// </summary>
        public int HighlightButton
        {
            get{
                if(IsHandleCreated)
                {
                    highlightButtonIndex = UiToolbar_IconGetHighLightButton(Handle);
                }
                return highlightButtonIndex;
            }
            set{
                // 如果高亮按钮索引超出范围，则不进行高亮
                if (value >= defaultButtonCount)
                    return;
                if(value!=highlightButtonIndex)
                {
                    highlightButtonIndex = value;
                    if(IsHandleCreated)
                    {
                        UiToolbar_IconSetHightLightButton(Handle, value);
                    }
                }
            }
        }
        #endregion

        #region 构造函数
        /// <summary>
        /// 构造图标工具条对象
        /// </summary>
        public IconToolBar()
        {
            Handle = UiToolbar_IconCtor();
            SetID();
            ButtonCount = 4;// 默认按钮数量为4
            HighlightButton = -1;// 默认不高亮按钮
            Location = new Point(0, Screen.ClientHeight - HEIGHT - 2);// 调整工具条位置。偏移量2
            Size = new Size(Screen.ClientWidth, HEIGHT);
        }
        #endregion

        #region 函数
        /// <summary>
        /// 设置按钮图标
        /// </summary>
        /// <param name="nIndex">按钮索引</param>
        /// <param name="hInst">图标资源所在DLL句柄</param>
        /// <param name="resType">图标资源类型</param>
        /// <param name="resId">图标资源Id</param>
        public void SetButtonIcon(int nIndex,IntPtr hInst,NativeResourceType resType,string resId)
        {
            if(IsHandleCreated)
            {
                string resTypeStr = ((int)resType).ToString();
                UiToolbar_IconLoadButtonIcon(Handle, nIndex, hInst, resTypeStr, resId);
            }
        }
        /// <summary>
        /// 设置按钮图标（禁用状态）
        /// </summary>
        /// <param name="nIndex">按钮索引</param>
        /// <param name="hInst">图标资源所在DLL句柄</param>
        /// <param name="resType">图标资源类型</param>
        /// <param name="resId">图标资源Id</param>
        public void SetButtonIcon_Disabled(int nIndex, IntPtr hInst, NativeResourceType resType, string resId)
        {
            if(IsHandleCreated)
            {
                string resTypeStr = ((int)resType).ToString();
                UiToolbar_IconLoadButtonIcon_Disabled(Handle, nIndex, hInst, resTypeStr, resId);
            }
        }
        /// <summary>
        /// 引发 ButtonClick 事件
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnButtonClicked(IconToolBarButtonClickedEventArgs e)
        {
            if(ButtonClicked!=null)
            {
                ButtonClicked(this, e);
            }
        }
        /// <summary>
        /// 处理MzFC 命令消息
        /// </summary>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        internal override void OnMzCommand(uint wParam, uint lParam)
        {
            int index = (int)lParam;

            OnButtonClicked(new IconToolBarButtonClickedEventArgs(index));
        }
        #endregion
    }
}
